layui.use(['table','upload'], function() {
var table = layui.table,
    $ = layui.jquery,
    layer = layui.layer,
    form = layui.form,
    laytpl = layui.laytpl,
    message = parent.message,
	upload = layui.upload;
var uploadInst1 = upload.render({
    elem: '#url',
    url: '/sys/baseuser/upload',
    accept: 'file',
    auto: false, //选择文件后不自动上传
    bindAction: '#url', //指向一个按钮触发上传
    choose: function(obj){
	    //将每次选择的文件追加到文件队列
    	 
	    var files = obj.pushFile();
	    //预读本地文件，如果是多文件，则会遍历。(不支持ie8/9)
	    obj.preview(function(index, file, result){
	      //这里还可以做一些 append 文件列表 DOM 的操作
	    	$('#url').attr('value', "http://47.94.220.217/comm-cg/basefile/"+file.name); 
	      fileFlg=true;
	      //obj.upload(index, file); //对上传失败的单个文件重新上传，一般在某个事件中使用
	      //delete files[index]; //删除列表中对应的文件，一般在某个事件中使用
	    });
	  }
    ,done: function(res){
      //如果上传失败
      if(res.code != 0){
    	  layer.msg(res.msg, {icon: 2});
      }else{
//    	  message.show({
//                skin:'default',//皮肤  支持:red,orange,cyan,blue,black,default
//                msg:'操作成功'//提示信息
//            });
    	 
      }
      //上传成功
    }
    ,error: function(){
      console.log("123");
    }
  });

var tableIns = table.render({
    elem: '#demo',
    height: 'full-132', //容器高度
    url: '../sys/baseuser/list?_'+$.now(),
    where: {sidx: '', order: 'desc'},
    page: true,
    id: 'demo',
    limit: 20,
    cols: [
        [{
            checkbox: true,
            fixed: true
        }, {
    		field : "headPic",
    		title : "头像",
    		width : 200,
    		templet: '#imgtpl'
    	}, {
		field : "userName",
		title : "姓名",
		width : 100,
		sort : true
	}, {
		field : "gender",
		title : "性别",
		width : 100,
		sort : true,
		templet: '#gendertpl'
	}, {
		field : "gridId",
		title : "网格编号",
		width : 100,
		sort : true
	}, {
		field : "buildingId",
		title : "楼栋编号",
		width : 100,
		sort : true
	}, {
		field : "buildingAddress",
		title : "楼栋地址",
		width : 100,
		sort : true
	}, {
		field : "householderRel",
		title : "与户主关系",
		width : 100,
		sort : true
	}, {
		field : "politicalStatus",
		title : "政治面貌",
		width : 100,
		sort : true,
		templet: '#politicalStatustpl'
	}, {
		field : "identifyNumber",
		title : "身份证号",
		width : 100,
		sort : true
	}, {
		field : "nation",
		title : "民族",
		width : 100,
		sort : true
	}, {
		field : "educationLevel",
		title : "文化水平",
		width : 100,
		sort : true
	}, {
		field : "maritalStatus",
		title : "婚姻状况",
		width : 100,
		sort : true,
		templet: '#maritalStatustpl'
	}, {
		field : "telephone",
		title : "电话",
		width : 100,
		sort : true
	}, {
		field : "workUnit",
		title : "工作单位",
        width: 100,
        sort: true
			
	}, {
		field : "militaryServiceStatus",
		title : "兵役状况",
        width: 100,
        sort: true,
        templet: '#militaryServiceStatustpl'
	}, {
		field : "enjoyLivingAllowancesFlg",
		title : "是否享受低保",
        width: 200,
        sort: true,
        templet: '#enjoyLivingAllowancesFlgtpl'
	}, 
	{
		field : "deformityFlg",
		title : "是否残疾",
        width: 100,
        sort: true,
        templet: '#deformityFlgtpl'
	}, 
	{
		field : "helpTargetFlg",
		title : "是否重点优扶对象",
        width: 200,
        sort: true,
        templet: '#helpTargetFlgtpl'
	}, 
	{
		field : "concernedDrugs",
		title : "涉毒情况",
        width: 100,
        sort: true,
        templet: '#concernedDrugsFlgtpl'
	}, 
	{
		field : "violationPeaceSituation",
		title : "违反治安情况行政拘留",
        width: 200,
        sort: true,
        templet: '#violationPeaceSituationtpl'
	}, 
	{
		field : "illegalSituation",
		title : "违法情况",
        width: 100,
        sort: true,
        templet: '#illegalSituationtpl'
	}, 
	{
		field : "joinCultFlg",
		title : "是否参加邪教组织",
        width: 200,
        sort: true,
        templet: '#joinCultFlgtpl'
	}, 
	{
		field : "endowmentInsurance",
		title : "养老保险",
        width: 100,
        sort: true,
        templet: '#endowmentInsuranceStrtpl'
	}, {
		field : "industrialInsurance",
		title : "工伤保险",
        width: 100,
        sort: true,
        templet: '#industrialInsuranceStrtpl'
	}, {
		field : "unemploymentInsurance",
		title : "失业保险",
        width: 100,
        sort: true,
        templet: '#unemploymentInsuranceStrtpl'
	}, {
		field : "medicalInsurance",
		title : "医疗保险",
        width: 100,
        sort: true,
        templet: '#medicalInsurancetpl'
	}, {
		field : "birthInsurance",
		title : "生育保险",
        width: 100,
        sort: true,
        templet: '#birthInsurancetpl'
	}, {
		field : "happinessFamilyRate",
		title : "幸福家庭五评比情况",
        width: 200,
        sort: true,
        templet: '#happinessFamilyRatetpl'
	}, {
		field : "exitingProblem",
		title : "存在问题",
        width: 100,
        sort: true
	}, {
		field : "rectificationFlg",
		title : "是否整改",
        width: 100,
        sort: true,
        templet: '#rectificationFlgtpl'
	}, {
		field : "enjoyServiceFlg",
		title : "享受服务情况",
        width: 100,
        sort: true,
        templet: '#enjoyServiceFlgtpl'
	},  {
            field: 'createTime',
            title: '创建时间',
            width: 200
        },{
            fixed: 'right',
            title: '操作',
            width: 200,
            align: 'center',
            toolbar: '#barDemo'
        }]
    ],
    done: function(res, curr, count) {
    	
    },
    loading: true,
});
form.render(null, 'kit-search-form');
$('#kit-search-more').on('click', function() {
    $('.kit-search-mored').toggle();
    
});

var liCTextarea,tiaoJTextarea,caiLTextarea,locationTextarea;
//监听工具条
table.on('tool(demo)', function(obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
    var data = obj.data; //获得当前行数据
    var layEvent = obj.event; //获得 lay-event 对应的值
    var tr = obj.tr; //获得当前行 tr 的DOM对象
    console.log(layEvent);
    if (layEvent === 'del') { //删除
        layer.confirm('确定删除该数据', function(index) {
            layer.close(index);
            //向服务端发送删除指令
            var userIds = Array();
            userIds.push(data.userId);
            $.ajax({
        		type: "POST",
        	    url: "../sys/baseuser/delete",
        	    data: JSON.stringify(userIds),
        	    dataType: 'JSON',
        	    contentType: 'application/json',
        	    success: function(res){
        	    	if (res.code==0) {
        	        	message.show({
        	                skin:'default',//皮肤  支持:red,orange,cyan,blue,black,default
        	                msg:'操作成功'//提示信息
        	            });
        	        	obj.del(); //删除对应行（tr）的DOM结构
        	        	//tableIns.reload({where: {sidx: '', order: 'desc'}});
        	        } else {
        	        	layer.msg(res.msg, {icon: 2});
        	        }
        		}
        	});
        });
    } else if (layEvent === 'edit') { //编辑
    	$.get("../sys/baseuser/info/"+data.id, function(res){
    		console.log(res);
			if(res.code==0){
				$.get("../sys/mapgrid/select?gridId="+res.userinfo.gridId, function(r){
					var d = {
						userinfo:{
							id:data.id,
							gridId:data.gridId,
							buildingId:data.buildingId,
							buildingAddress:data.buildingAddress,
							userName:data.userName,
							permanentAddress:data.permanentAddress,
							homeAddress:data.homeAddress,
							gender:data.gender,
							householderRel:data.householderRel,
							politicalStatus:data.politicalStatus,
							identifyNumber:data.identifyNumber,
							nation:data.nation,
							educationLevel:data.educationLevel,
							maritalStatus:data.maritalStatus,
							telephone:data.telephone,
							workUnit:data.workUnit,
							militaryServiceStatus:data.militaryServiceStatus,
							enjoyLivingAllowancesFlg:data.enjoyLivingAllowancesFlg,
							deformityFlg:data.deformityFlg,
							deformityType:data.deformityType,
							deformityLevel:data.deformityLevel,
							helpTargetFlg:data.helpTargetFlg,
							helpTargetType:data.helpTargetType,
							concernedDrugs:data.concernedDrugs,
							violationPeaceSituation:data.violationPeaceSituation,
							illegalSituation:data.illegalSituation,
							joinCultFlg:data.joinCultFlg,
							cultName:data.cultName,
							endowmentInsurance:data.endowmentInsurance,
							industrialInsurance:data.industrialInsurance,
							unemploymentInsurance:data.unemploymentInsurance,
							medicalInsurance:data.medicalInsurance,
							happinessFamilyRate:data.happinessFamilyRate,
							exitingProblem:data.exitingProblem,
							rectificationFlg:data.rectificationFlg,
							enjoyServiceFlg:data.enjoyServiceFlg,
							headPic:data.headPic
						},
						mapgridList:r.list,
						buildingList:r.buildingList
			        };
					var fileFlg = false;
			        laytpl($('#edit-tpl').html()).render(d, function(html) {
			            layer.open({
			                type: 1,
			                title: '人员信息',
			                content: html,
			                maxmin: true,
			                maxHeight:$("body").height()*0.8,
			                anim:2,
			                area: '800px',
			                btn: ['提交', '取消'],
			                yes: function(index, layero) {
			                    editIndex = index;
			                    $('form[lay-filter="form-edit"]').find('button[lay-submit]').click();
			                    var _headPic = $("#headPic").attr("src");
			                    var flag=false;
			                    var userName =$("#userName").val();
			                    var householderRel=$("#householderRel").val();
			                    var flag = true;
			                    var msg="";
			                   
			                    var buildingAddress=$("#buildingAddress").val();
			                    var permanentAddress=$("#permanentAddress").val();
			                    var homeAddress = $("#homeAddress").val();
			                    var nation= $("#nation").val();
			                    var educationLevel = $("#educationLevel").val();
			                    var identifyNumber = $("#identifyNumber").val();
			                    var telephone = $("#telephone").val();
			                    if(userName==''||userName=='null'){
			                    	$("#userName").attr("class", "layui-input layui-form-danger"); 	
			                    	$("#userName").focus(); 
			                    	msg="用户名不能为空";
			                    	flag=false;
			                    }
			                    else if(householderRel==''||householderRel=='null'){
			                    	$("#householderRel").attr("class", "layui-input layui-form-danger"); 	
			                    	$("#householderRel").focus(); 
			                    	msg="与户主关系不能为空";
			                    	flag=false;
			                    }
			                    else   if(buildingAddress==''||buildingAddress=='null'){
			                    	$("#buildingAddress").attr("class", "layui-input layui-form-danger"); 	
			                    	$("#buildingAddress").focus(); 
			                    	msg="楼栋地址不能为空";
			                    	flag=false;
			                    }
			                    
			                    else if(permanentAddress==''||permanentAddress=='null'){
			                    	$("#permanentAddress").attr("class", "layui-input layui-form-danger"); 	
			                    	$("#permanentAddress").focus(); 
			                    	msg="户籍地址不能为空";
			                    	flag=false;
			                    }
			                    
			                    else  if(homeAddress==''||homeAddress=='null'){
			                    	$("#homeAddress").attr("class", "layui-input layui-form-danger"); 	
			                    	$("#homeAddress").focus(); 
			                    	msg="家庭地址不能为空";
			                    	flag=false;
			                    }
			                    else  if(nation==''||nation=='null'){
			                    	$("#nation").attr("class", "layui-input layui-form-danger"); 	
			                    	$("#nation").focus(); 
			                    	msg="民族不能为空";
			                    	flag=false;
			                    }
			                    else  if(educationLevel==''||educationLevel=='null'){
			                    	$("#educationLevel").attr("class", "layui-input layui-form-danger"); 	
			                    	$("#educationLevel").focus(); 
			                    	msg="文化水平不能为空";
			                    	flag=false;
			                    }
			                    else   if(identifyNumber==''||identifyNumber=='null'){
			                    	$("#identifyNumber").attr("class", "layui-input layui-form-danger"); 	
			                    	$("#identifyNumber").focus(); 
			                    	msg="身份证号不能为空";
			                    	flag=false;
			                    }
			                    
			                    else  if(telephone==''||telephone=='null'){
			                    	$("#telephone").attr("class", "layui-input layui-form-danger"); 	
			                    	$("#telephone").focus(); 
			                    	msg="电话号不能为空";
			                    	flag=false;
			                    }
			                    
			                    if(flag){
			                    	if(fileFlg){
	    	                    		uploadInst.config.data={id:$("#id").val(),gridId:$("#gridId").val(),buildingId:$("#buildingId").val(),buildingAddress:$("#buildingAddress").val(),
	    	                    				userName:userName,permanentAddress:permanentAddress,homeAddress:$("#homeAddress").val(),
	    	                    				gender:$("#gender").val(),householderRel:householderRel,politicalStatus:$("#politicalStatus").val(),identifyNumber:$("#identifyNumber").val(),
	    	                    				nation:$("#nation").val(),educationLevel:$("#educationLevel").val(),maritalStatus:$("#maritalStatus").val(),telephone:$("#telephone").val(),
	    	                    				workUnit:$("#workUnit").val(),militaryServiceStatus:$("#militaryServiceStatus").val(),enjoyLivingAllowancesFlg:$("#enjoyLivingAllowancesFlg").val(),deformityFlg:$("#deformityFlg").val(),
	    	                    				deformityType:$("#deformityType").val(),deformityLevel:$("#deformityLevel").val(),helpTargetFlg:$("#helpTargetFlg").val()
	    	                    				,helpTargetType:$("#helpTargetType").val(),concernedDrugs:$("#concernedDrugs").val(),violationPeaceSituation:$("#violationPeaceSituation").val()
	    	                    				,illegalSituation:$("#illegalSituation").val(),joinCultFlg:$("#joinCultFlg").val(),cultName:$("#cultName").val()
	    	                    				,endowmentInsurance:$("#endowmentInsurance").val(),industrialInsurance:$("#industrialInsurance").val(),unemploymentInsurance:$("#unemploymentInsurance").val()
	    	                    				,medicalInsurance:$("#medicalInsurance").val(),exitingProblem:$("#exitingProblem").val()
	    	                    				,happinessFamilyRate:$("#happinessFamilyRate").val(),rectificationFlg:$("#rectificationFlg").val()
	    	                    				,enjoyServiceFlg:$("#enjoyServiceFlg").val(),headPic:$("#headPic").val()} 
				                    	$('#formEdit').click();
	    	                    	}else{
	    	                    		var baseuser = new Object(); 
	    	                    		baseuser.id = $("#id").val(); 
	    	                    		baseuser.gridId = $("#gridId").val(); 
	    	                    		baseuser.buildingId = $("#buildingId").val(); 
	    	                    		baseuser.buildingAddress = $("#buildingAddress").val(); 
	    	                    		baseuser.userName = userName; baseuser.permanentAddress = $("#permanentAddress").val(); 
	    	                    		baseuser.homeAddress = $("#homeAddress").val(); baseuser.gender = $("#gender").val(); 
	    	                    		baseuser.householderRel = householderRel; baseuser.politicalStatus = $("#politicalStatus").val(); 
	    	                    		baseuser.identifyNumber = $("#identifyNumber").val(); baseuser.nation = $("#nation").val(); 
	    	                    		baseuser.educationLevel = $("#educationLevel").val(); baseuser.maritalStatus = $("#maritalStatus").val(); 
	    	                    		baseuser.workUnit = $("#workUnit").val(); baseuser.militaryServiceStatus = $("#militaryServiceStatus").val(); 
	    	                    		baseuser.telephone = $("#telephone").val();baseuser.deformityFlg = $("#deformityFlg").val();
	    	                    		baseuser.enjoyLivingAllowancesFlg = $("#enjoyLivingAllowancesFlg").val(); baseuser.enjoyLivingAllowancesFlg = $("#enjoyLivingAllowancesFlg").val(); 
	    	                    		baseuser.deformityType = $("#deformityType").val();baseuser.deformityLevel = $("#deformityLevel").val();
	    	                    		baseuser.helpTargetFlg = $("#helpTargetFlg").val();baseuser.helpTargetType = $("#helpTargetType").val();
	    	                    		baseuser.concernedDrugs = $("#concernedDrugs").val();baseuser.violationPeaceSituation = $("#violationPeaceSituation").val();
	    	                    		baseuser.illegalSituation = $("#illegalSituation").val();baseuser.joinCultFlg = $("#joinCultFlg").val();
	    	                    		baseuser.cultName = $("#cultName").val();baseuser.endowmentInsurance = $("#endowmentInsurance").val();
	    	                    		baseuser.industrialInsurance = $("#industrialInsurance").val();baseuser.unemploymentInsurance = $("#unemploymentInsurance").val();
	    	                    		baseuser.medicalInsurance = $("#medicalInsurance").val();baseuser.exitingProblem = $("#exitingProblem").val();
	    	                    		baseuser.happinessFamilyRate = $("#happinessFamilyRate").val();baseuser.rectificationFlg = $("#rectificationFlg").val();
	    	                    		baseuser.enjoyServiceFlg = $("#enjoyServiceFlg").val();baseuser.headPic = $("#headPic").val();
//	    	                    		var json = JSON.stringify(baseuser); 
	    	                    		saveOrUpdate(baseuser);
	    	                    	}
		                    		layer.close(index); //关闭弹出层
			                    	
			                    }
			                    else{
    	                    		layer.msg(msg, {icon: 2});
    	                    	}
			                    
			                },
			                btn2: function(index, layero) {
			                	layer.close(index); //关闭弹出层
			                    $('form[lay-filter="form-edit"]').find('button[type="reset"]').click();
			                    return false;
			                },
			                success: function() {
			                    form.render(null, 'form-edit');
			                    
			                    
			                }
			            });
			            form.on('select(bjType)', function(data){
			            	$.get("../sys/mapbuilding/select?gridId="+data.value, function(res){
			            		d=res.list;
			            		console.log(res);
				            		  $("#buildingId").html('');
				            		  for(var i=0;i<d.length;i++){
				            				  if(data.buildingId==d[i].id){
				            					  $("#buildingId").append('<option value= '+d[i].id+' selected>'+d[i].name+'</option>');
				            				  }
				            				  else{
				            					  $("#buildingId").append('<option value= '+d[i].id+'>'+d[i].name+'</option>');
				            				  }
				            		  }
			            		form.render('select'); //更新 lay-filter="bjContents" 所在容器内的全部 select 状态
			            	});
		            	});
			            var uploadInst = upload.render({
	                        elem: '#menuLogoButton',
	                        url: '/comm-cg/sys/baseuser/update2',
	                        accept: 'images',
	                        auto: false, //选择文件后不自动上传
	                        bindAction: '#formEdit', //指向一个按钮触发上传
	                        choose: function(obj){
	                    	    //将每次选择的文件追加到文件队列
	                    	    var files = obj.pushFile();
	                    	    //预读本地文件，如果是多文件，则会遍历。(不支持ie8/9)
	                    	    obj.preview(function(index, file, result){
	                    	      //这里还可以做一些 append 文件列表 DOM 的操作
	                    	      $('#headPic').attr('src', result); //图片链接（base64）
	                    	      fileFlg=true;
	                    	      //obj.upload(index, file); //对上传失败的单个文件重新上传，一般在某个事件中使用
	                    	      //delete files[index]; //删除列表中对应的文件，一般在某个事件中使用
	                    	    });
	                    	  }
	                        ,done: function(res){
	                          //如果上传失败
	                          if(res.code != 0){
	                        	  layer.msg(res.msg, {icon: 2});
	                          }else{
	                        	  message.show({
	              	                skin:'default',//皮肤  支持:red,orange,cyan,blue,black,default
	              	                msg:'操作成功'//提示信息
	              	            });
	              	        	tableIns.reload({where: {order: 'desc'}});
	                          }
	                          //上传成功
	                        }
	                        ,error: function(){
	                          
	                        }
	                      });
			        });
				});
			}else {
	        	layer.msg(res.msg, {icon: 2});
	        }
		});
    }
    // 
    else if (layEvent === 'import') { 
    	alert('123');
    	 laytpl($('#data-tpl').html()).render(d, function(html) {
	            layer.open({
	                type: 1,
	                title: '人员信息',
	                content: html,
	                maxmin: true,
	                maxHeight:$("body").height()*0.8,
	                anim:2,
	                area: '800px',
	                btn: ['提交', '取消'],
	                yes: function(index, layero) {
	                    editIndex = index;
	                    $('form[lay-filter="form-edit"]').find('button[lay-submit]').click();
	                    
	                    
	                    
                 		layer.msg(msg, {icon: 2});
	                    
	                    
	                },
	                btn2: function(index, layero) {
	                	layer.close(index); //关闭弹出层
	                    $('form[lay-filter="form-edit"]').find('button[type="reset"]').click();
	                    return false;
	                },
	                success: function() {
	                    form.render(null, 'form-edit');
	                    
	                    
	                }
	            });
    	 });
    }
    
    else if (layEvent === 'export') {//
        //向服务端发送删除指令
        var userIds = Array();
        userIds.push(data.userId);
        $.ajax({
    		type: "POST",
    	    url: "../sys/baseuser/export",
    	    data: JSON.stringify(userIds),
    	    dataType: 'JSON',
    	    contentType: 'application/json',
    	    success: function(res){
    	    	if (res.code==0) {
    	        	message.show({
    	                skin:'default',//皮肤  支持:red,orange,cyan,blue,black,default
    	                msg:'操作成功'//提示信息
    	            });
    	        	obj.del(); //删除对应行（tr）的DOM结构
    	        	//tableIns.reload({where: {sidx: '', order: 'desc'}});
    	        } else {
    	        	layer.msg(res.msg, {icon: 2});
    	        }
    		}
    	});
    }
});

var roleList;
$(function(){
	$.get("../sys/role/select", function(r){
		roleList = r.list;
	});
});

$('.kit-search-btns').children('a').off('click').on('click', function() {
    var $that = $(this),
    action = $that.data('action');
    switch (action) {
        case 'add':
        	$.get("../sys/businessdoc/select", function(r){
        		if(r.code==0){
        			$.get("../sys/mapgrid/select", function(r){
        			var d = {
        					userinfo:{
    							id:'',
    							gridId:'',
    							buildingId:'',
    							buildingAddress:'',
    							userName:'',
    							permanentAddress:'',
    							homeAddress:'',
    							gender:'',
    							householderRel:'',
    							politicalStatus:'',
    							identifyNumber:'',
    							nation:'',
    							educationLevel:'',
    							maritalStatus:'',
    							telephone:'',
    							workUnit:'',
    							militaryServiceStatus:'',
    							enjoyLivingAllowancesFlg:'',
    							deformityFlg:'',
    							deformityType:'',
    							deformityLevel:'',
    							helpTargetFlg:'',
    							helpTargetType:'',
    							concernedDrugs:'',
    							violationPeaceSituation:'',
    							illegalSituation:'',
    							joinCultFlg:'',
    							cultName:'',
    							endowmentInsurance:'',
    							industrialInsurance:'',
    							unemploymentInsurance:'',
    							medicalInsurance:'',
    							medicalInsurance:'',
    							happinessFamilyRate:'',
    							exitingProblem:'',
    							rectificationFlg:'',
    							enjoyServiceFlg:'',
    							headPic:''
    						},
    						mapgridList:r.list,
    						buildingList:r.buildingList
                    };
        			var fileFlg = false;
                    //渲染
                    laytpl($('#edit-tpl').html()).render(d,
                        function(html) {
                            layer.open({
                                type: 1,
                                title: '新增',
                                content: html,
                                skin: 'layui-layer-rim', //加上边框
                                area: '800px',
    			                maxHeight:$("body").height()*0.8,
                                btn: ['提交', '取消'],
                                yes: function(index, layero) {
                                    editIndex = index;
                                    $('form[lay-filter="form-edit"]').find('button[lay-submit]').click();
                                    var _headPic = $("#headPic").attr("src");
                                    var userName =$("#userName").val();
    			                    var householderRel=$("#householderRel").val();
    			                    var flag = true;
    			                    var msg="";
    			                    var buildingAddress=$("#buildingAddress").val();
    			                    var permanentAddress=$("#permanentAddress").val();
    			                    var homeAddress = $("#homeAddress").val();
    			                    var nation= $("#nation").val();
    			                    var educationLevel = $("#educationLevel").val();
    			                    var identifyNumber = $("#identifyNumber").val();
    			                    var telephone = $("#telephone").val();
    			                    if(userName==''||userName=='null'){
    			                    	$("#userName").attr("class", "layui-input layui-form-danger"); 	
    			                    	$("#userName").focus(); 
    			                    	msg="用户名不能为空";
    			                    	flag=false;
    			                    }
    			                    else if(householderRel==''||householderRel=='null'){
    			                    	$("#householderRel").attr("class", "layui-input layui-form-danger"); 	
    			                    	$("#householderRel").focus(); 
    			                    	msg="与户主关系不能为空";
    			                    	flag=false;
    			                    }
    			                    else   if(buildingAddress==''||buildingAddress=='null'){
    			                    	$("#buildingAddress").attr("class", "layui-input layui-form-danger"); 	
    			                    	$("#buildingAddress").focus(); 
    			                    	msg="楼栋地址不能为空";
    			                    	flag=false;
    			                    }
    			                    
    			                    else if(permanentAddress==''||permanentAddress=='null'){
    			                    	$("#permanentAddress").attr("class", "layui-input layui-form-danger"); 	
    			                    	$("#permanentAddress").focus(); 
    			                    	msg="户籍地址不能为空";
    			                    	flag=false;
    			                    }
    			                    
    			                    else  if(homeAddress==''||homeAddress=='null'){
    			                    	$("#homeAddress").attr("class", "layui-input layui-form-danger"); 	
    			                    	$("#homeAddress").focus(); 
    			                    	msg="家庭地址不能为空";
    			                    	flag=false;
    			                    }
    			                    else  if(nation==''||nation=='null'){
    			                    	$("#nation").attr("class", "layui-input layui-form-danger"); 	
    			                    	$("#nation").focus(); 
    			                    	msg="民族不能为空";
    			                    	flag=false;
    			                    }
    			                    else  if(educationLevel==''||educationLevel=='null'){
    			                    	$("#educationLevel").attr("class", "layui-input layui-form-danger"); 	
    			                    	$("#educationLevel").focus(); 
    			                    	msg="文化水平不能为空";
    			                    	flag=false;
    			                    }
    			                    else   if(identifyNumber==''||identifyNumber=='null'){
    			                    	$("#identifyNumber").attr("class", "layui-input layui-form-danger"); 	
    			                    	$("#identifyNumber").focus(); 
    			                    	msg="身份证号不能为空";
    			                    	flag=false;
    			                    }
    			                    
    			                    
    			                    else  if(telephone==''||telephone=='null'){
    			                    	$("#telephone").attr("class", "layui-input layui-form-danger"); 	
    			                    	$("#telephone").focus(); 
    			                    	msg="电话号不能为空";
    			                    	flag=false;
    			                    }
    			                    
    			                    if(flag){
	    			                    if(fileFlg){
	        	                    		uploadInst.config.data={gridId:$("#gridId").val(),buildingId:$("#buildingId").val(),buildingAddress:$("#buildingAddress").val(),
	        	                    				userName:$("#userName").val(),permanentAddress:$("#permanentAddress").val(),homeAddress:$("#homeAddress").val(),
	        	                    				gender:$("#gender").val(),householderRel:$("#householderRel").val(),politicalStatus:$("#politicalStatus").val(),identifyNumber:$("#identifyNumber").val(),
	        	                    				nation:$("#nation").val(),educationLevel:$("#educationLevel").val(),maritalStatus:$("#maritalStatus").val(),telephone:$("#telephone").val(),
	        	                    				workUnit:$("#workUnit").val(),militaryServiceStatus:$("#militaryServiceStatus").val(),enjoyLivingAllowancesFlg:$("#enjoyLivingAllowancesFlg").val(),deformityFlg:$("#deformityFlg").val(),
	        	                    				deformityType:$("#deformityType").val(),deformityLevel:$("#deformityLevel").val(),helpTargetFlg:$("#helpTargetFlg").val()
	        	                    				,helpTargetType:$("#helpTargetType").val(),concernedDrugs:$("#concernedDrugs").val(),violationPeaceSituation:$("#violationPeaceSituation").val()
	        	                    				,illegalSituation:$("#illegalSituation").val(),joinCultFlg:$("#joinCultFlg").val(),cultName:$("#cultName").val()
	        	                    				,endowmentInsurance:$("#endowmentInsurance").val(),industrialInsurance:$("#industrialInsurance").val(),unemploymentInsurance:$("#unemploymentInsurance").val()
	        	                    				,medicalInsurance:$("#medicalInsurance").val(),exitingProblem:$("#exitingProblem").val()
	        	                    				,happinessFamilyRate:$("#happinessFamilyRate").val(),rectificationFlg:$("#rectificationFlg").val()
	        	                    				,enjoyServiceFlg:$("#enjoyServiceFlg").val(),headPic:$("#headPic").val()} 
	    			                    	$('#formEdit').click();
	        	                    	}else{
	        	                    		var baseuser = new Object(); 
	        	                    		baseuser.gridId = $("#gridId").val(); 
	        	                    		baseuser.buildingId = $("#buildingId").val(); 
	        	                    		baseuser.buildingAddress = $("#buildingAddress").val(); 
	        	                    		baseuser.userName = $("#userName").val(); baseuser.permanentAddress = $("#permanentAddress").val(); 
	        	                    		baseuser.homeAddress = $("#homeAddress").val(); baseuser.gender = $("#gender").val(); 
	        	                    		baseuser.householderRel = $("#householderRel").val(); baseuser.politicalStatus = $("#politicalStatus").val(); 
	        	                    		baseuser.identifyNumber = $("#identifyNumber").val(); baseuser.nation = $("#nation").val(); 
	        	                    		baseuser.educationLevel = $("#educationLevel").val(); baseuser.maritalStatus = $("#maritalStatus").val(); 
	        	                    		baseuser.workUnit = $("#workUnit").val(); baseuser.militaryServiceStatus = $("#militaryServiceStatus").val(); 
	        	                    		baseuser.telephone = $("#telephone").val();baseuser.deformityFlg = $("#deformityFlg").val();
	        	                    		baseuser.enjoyLivingAllowancesFlg = $("#enjoyLivingAllowancesFlg").val(); baseuser.enjoyLivingAllowancesFlg = $("#enjoyLivingAllowancesFlg").val(); 
	        	                    		baseuser.deformityType = $("#deformityType").val();baseuser.deformityLevel = $("#deformityLevel").val();
	        	                    		baseuser.helpTargetFlg = $("#helpTargetFlg").val();baseuser.helpTargetType = $("#helpTargetType").val();
	        	                    		baseuser.concernedDrugs = $("#concernedDrugs").val();baseuser.violationPeaceSituation = $("#violationPeaceSituation").val();
	        	                    		baseuser.illegalSituation = $("#illegalSituation").val();baseuser.joinCultFlg = $("#joinCultFlg").val();
	        	                    		baseuser.cultName = $("#cultName").val();baseuser.endowmentInsurance = $("#endowmentInsurance").val();
	        	                    		baseuser.industrialInsurance = $("#industrialInsurance").val();baseuser.unemploymentInsurance = $("#unemploymentInsurance").val();
	        	                    		baseuser.medicalInsurance = $("#medicalInsurance").val();baseuser.exitingProblem = $("#exitingProblem").val();
	        	                    		baseuser.happinessFamilyRate = $("#happinessFamilyRate").val();baseuser.rectificationFlg = $("#rectificationFlg").val();
	        	                    		baseuser.enjoyServiceFlg = $("#enjoyServiceFlg").val();baseuser.headPic = $("#headPic").val();
	//        	                    		var json = JSON.stringify(baseuser); 
	        	                    		saveOrUpdate(baseuser);
	        	                    	}
    			                    layer.close(index); //关闭弹出层
    			                    }
    			                    else{
    			                    	layer.msg(msg, {icon: 2});
    			                    }
    			                    
                                },
                                btn2: function(index, layero) {
                                	layer.close(index); //关闭弹出层
                                    $('form[lay-filter="form-edit"]').find('button[type="reset"]').click();
                                    return false;
                                },
                                success: function() {
                                    form.render(null, 'form-edit');
                                }
                            });
                        });
                    form.on('select(bjType)', function(data){
		            	$.get("../sys/mapbuilding/select?gridId="+data.value, function(res){
		            		d=res.list;
		            		console.log(res);
			            		  $("#buildingId").html('');
			            		  for(var i=0;i<d.length;i++){
			            				  if(data.buildingId==d[i].id){
			            					  $("#buildingId").append('<option value= '+d[i].id+' selected>'+d[i].name+'</option>');
			            				  }
			            				  else{
			            					  $("#buildingId").append('<option value= '+d[i].id+'>'+d[i].name+'</option>');
			            				  }
			            		  }
		            		form.render('select'); //更新 lay-filter="bjContents" 所在容器内的全部 select 状态
		            	});
	            	});
                    var uploadInst = upload.render({
                        elem: '#menuLogoButton',
                        url: '/comm-cg/sys/baseuser/save2',
                        accept: 'images',
                        auto: false, //选择文件后不自动上传
                        bindAction: '#formEdit', //指向一个按钮触发上传
                        choose: function(obj){
                    	    //将每次选择的文件追加到文件队列
                    	    var files = obj.pushFile();
                    	    //预读本地文件，如果是多文件，则会遍历。(不支持ie8/9)
                    	    obj.preview(function(index, file, result){
                    	      //这里还可以做一些 append 文件列表 DOM 的操作
                    	      $('#headPic').attr('src', result); //图片链接（base64）
                    	      fileFlg=true;
                    	      //obj.upload(index, file); //对上传失败的单个文件重新上传，一般在某个事件中使用
                    	      //delete files[index]; //删除列表中对应的文件，一般在某个事件中使用
                    	    });
                    	  }
                        ,done: function(res){
                          //如果上传失败
                          if(res.code != 0){
                        	  layer.msg(res.msg, {icon: 2});
                          }else{
                        	  message.show({
              	                skin:'default',//皮肤  支持:red,orange,cyan,blue,black,default
              	                msg:'操作成功'//提示信息
              	            });
              	        	tableIns.reload({where: {order: 'desc'}});
                          }
                          //上传成功
                        }
                        ,error: function(){
                          
                        }
                      });
        			});
        		}else {
    	        	layer.msg(r.msg, {icon: 2});
    	        }
        	});
            break;
        case 'del-bulk':
            var d = table.checkStatus('demo');
            if (d.data.length === 0) {
                layer.msg('请选择要删除的数据');
                return;
            }
            var data = d.data,
                ids = [];
            layui.each(data, function(index, item) {
                ids.push(item.id);
            });
            layer.confirm('确定删除选中的数据', function(index) {
            	layer.close(index);
                //向服务端发送删除指令
                $.ajax({
            		type: "POST",
            	    url: "../sys/baseuser/delete",
            	    data: JSON.stringify(ids),
            	    dataType: 'JSON',
            	    contentType: 'application/json',
            	    success: function(res){
            	    	if (res.code==0) {
            	        	message.show({
            	                skin:'default',//皮肤  支持:red,orange,cyan,blue,black,default
            	                msg:'操作成功'//提示信息
            	            });
            	        	tableIns.reload({where: {sidx: '', order: 'desc'}});
            	        } else {
            	        	layer.msg(res.msg, {icon: 2});
            	        }
            		}
            	});
            });
            break;
        }
    });

var saveOrUpdate = function (datas) {
	console.log(datas);
	var url = datas.id == null ? "../sys/baseuser/save" : "../sys/baseuser/update";
	$.ajax({
		type: "POST",
	    url: url,
	    data: JSON.stringify(datas),
	    dataType: 'JSON',
	    contentType: 'application/json',
	    success: function(res){
	    	if (res.code==0) {
	        	message.show({
	                skin:'default',//皮肤  支持:red,orange,cyan,blue,black,default
	                msg:'操作成功'//提示信息
	            });
	        	tableIns.reload({where: {sidx: '', order: 'desc'}});
	        } else {
	        	layer.msg(res.msg, {icon: 2});
	        }
		}
	});
}

//监听搜索表单提交
form.on('submit(search)', function(data) {
	if(data.field.mobile!=""||data.field.status!=""||data.field.email!=""){
		$('.kit-search-mored').toggle();
	}
    if(data.field.status==""){
    	//带条件查询
        tableIns.reload({
        	where: {
        		sidx: '', order: 'desc',
        		userName: data.field.userName,
        		politicalStatus: data.field.politicalStatus,
        		telephone: data.field.telephone,
        		deformityFlg: data.field.deformityFlg
        	}
        });
    }else{
    	//带条件查询
        tableIns.reload({
        	where: {
        		sidx: '', order: 'desc',
        		userName: data.field.userName,
        		politicalStatus: data.field.politicalStatus,
        		telephone: data.field.telephone,
        		deformityFlg: data.field.deformityFlg
        	}
        });
    }
    return false;
});
var editIndex;
//form.on('submit(formEdit)', function(data) {
//	
//	saveOrUpdate(data.field);
//	editIndex && layer.close(editIndex); //关闭弹出层
//    return false;
//});

});
